﻿package boy.contrib.net
{
    import flash.net.*;
    import flash.utils.*;

    public class URLRequestBuilder extends Object
    {
        private var variables:URLVariables;
        private static const MULTIPART_MARK:String = "--";
        private static const LF:String = "\r\n";
        private static const MULTIPART_BOUNDARY:String = "----------196f00b77b968397849367c61a2080";

        public function URLRequestBuilder(param1:URLVariables)
        {
            this.variables = param1;
            return;
        }// end function

        private function buildMultipartBody() : ByteArray
        {
            var _loc_2:String;
            var _loc_1:* = new ByteArray();
            for (_loc_2 in this.variables)
            {
                // label
                _loc_1.writeBytes(this.encodeMultipartVariable(_loc_2, this.variables[_loc_2]));
            }// end of for ... in
            _loc_1.writeUTFBytes(MULTIPART_MARK + MULTIPART_BOUNDARY + LF);
            _loc_1.writeUTFBytes(MULTIPART_MARK + MULTIPART_BOUNDARY + MULTIPART_MARK);
            return _loc_1;
        }// end function

        private function encodeMultipartVariable(param1:String, param2:Object) : ByteArray
        {
            if (param2 is URLFileVariable)
            {
                return this.encodeMultipartFile(param1, URLFileVariable(param2));
            }// end if
            return this.encodeMultipartString(param1, param2.toString());
        }// end function

        private function encodeMultipartString(param1:String, param2:String) : ByteArray
        {
            var _loc_3:* = new ByteArray();
            _loc_3.writeUTFBytes(MULTIPART_MARK + MULTIPART_BOUNDARY + LF + "Content-Disposition: form-data; name=\"" + param1 + "\"" + LF + LF + param2 + LF);
            return _loc_3;
        }// end function

        private function encodeMultipartFile(param1:String, param2:URLFileVariable) : ByteArray
        {
            var _loc_3:* = new ByteArray();
            _loc_3.writeUTFBytes(MULTIPART_MARK + MULTIPART_BOUNDARY + LF + "Content-Disposition: form-data; name=\"" + param1 + "\"; " + "filename=\"" + param2.name + "\"" + LF + "Content-Type: application/octet-stream" + LF + LF);
            _loc_3.writeBytes(param2.data);
            _loc_3.writeUTFBytes(LF);
            return _loc_3;
        }// end function

        private function get isMultipartData() : Boolean
        {
            var _loc_1:*;
            for each (_loc_1 in this.variables)
            {
                // label
                if (_loc_1 is URLFileVariable)
                {
                    return true;
                }// end if
            }// end of for each ... in
            return false;
        }// end function

        private function addMultipartHeadersTo(param1:URLRequest) : void
        {
            param1.method = URLRequestMethod.POST;
            param1.contentType = "multipart/form-data; boundary=" + MULTIPART_BOUNDARY;
            param1.requestHeaders = [new URLRequestHeader("Accept", "*/*"), new URLRequestHeader("Cache-Control", "no-cache")];
            return;
        }// end function

        public function build() : URLRequest
        {
            var _loc_1:* = new URLRequest();
            if (this.isMultipartData)
            {
                _loc_1.data = this.buildMultipartBody();
                this.addMultipartHeadersTo(_loc_1);
            }
            else
            {
                _loc_1.data = this.variables;
            }// end else if
            return _loc_1;
        }// end function

    }
}
